package org.etlt.extract;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.commons.lang3.StringUtils;
import org.etlt.SettingCheck;
import org.etlt.SettingValidationException;
import org.etlt.load.ColumnSetting;

import java.util.ArrayList;
import java.util.List;

import static org.etlt.extract.ExtractorSetting.DATA_BASE_TYPE;
import static org.etlt.extract.ExtractorSetting.FILE_TYPE;

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = FileExtractSetting.class, name = FILE_TYPE),
        @JsonSubTypes.Type(value = DatabaseExtractSetting.class, name = DATA_BASE_TYPE)})
public class ExtractorSetting implements SettingCheck {
    public static final String DATA_BASE_TYPE = "DATA_BASE";

    public static final String BIG_DATA_BASE_TYPE = "BIG_DATA_BASE";
    public static final String FILE_TYPE = "FILE";
    public static final String UTF_8 = "utf8";

    private String name;

    private int skip;

    private boolean parallel = false;

    private List<String> columns = new ArrayList<String>();

    private List<ColumnSetting> columnSettings = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSkip() {
        return skip;
    }

    public void setSkip(int skip) {
        this.skip = skip;
    }

    public List<String> getColumns() {
        if(this.columns.isEmpty()){
            getColumnSettings().forEach(columnSetting -> {
                this.columns.add(columnSetting.getName());
            });
        }
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public List<ColumnSetting> getColumnSettings() {
        return columnSettings;
    }

    public void setColumnSettings(List<ColumnSetting> columnSettings) {
        this.columnSettings = columnSettings;
    }

    /**
     * NOT supported now.
     * @return
     */
    public boolean isParallel() {
        return parallel;
    }
    @Deprecated
    public void setParallel(boolean parallel) {
        this.parallel = parallel;
    }
    @Override
    public void check() {
        assertName("name",getName());
        if(StringUtils.isBlank(getName()))
            throw new SettingValidationException("extractor name missing." );
        if(StringUtils.isBlank(getName()))
            throw new SettingValidationException("datasource definition missing: " + getName() );
    }
}
